#include "../include/head.h"
#include "../include/serverLogin.h"

/* Usage*/

int mysql_userInfo_Insert_userName(char *useName){

    MYSQL *mysql= mysql_init(NULL);   //申请空间
    MYSQL *res=mysql_real_connect(mysql,   //链接数据库
                                  "localhost",
                                  "root",
                                  "123",
                                  "userInfo",
                                  0,NULL,0);
    if(res==NULL){       //判断数据库链接
        fprintf(stderr,"%s\n",mysql_error(mysql));
        exit(-1);
    }
    char sql[4096]={0};      //用来存储select命令
    sprintf(sql,"insert into userInfo(username) value('%s');",useName);
    int qres = mysql_query(mysql,sql);
    if(qres != 0)
    {   
        fprintf(stderr,"%s:%s\n","mysql_query",mysql_error(mysql));
        exit(-1);
    }   
    mysql_close(mysql);
    return 0;
}
int mysql_file_Insert_userName(char *useName){

    MYSQL *mysql= mysql_init(NULL);   //申请空间
    MYSQL *res=mysql_real_connect(mysql,   //链接数据库
                                  "localhost",
                                  "root",
                                  "123",
                                  "test",
                                  0,NULL,0);
    if(res==NULL){       //判断数据库链接
        fprintf(stderr,"%s\n",mysql_error(mysql));
        exit(-1);
    }   
    char sql[4096]={0};      //用来存储select命令
    sprintf(sql,"insert into file(filename,username,parent_id,path,type) value('/','%s',-1,'/','d');",useName);
    int qres = mysql_query(mysql,sql);
    if(qres != 0)
    {   
        fprintf(stderr,"%s:%s\n","mysql_query",mysql_error(mysql));
        exit(-1);
    }   
    mysql_close(mysql);
    return 0;
}

int mysql_userInfo_Insert_salt(char *salt,char *username){    

    MYSQL *mysql= mysql_init(NULL);   //申请空间                            
    MYSQL *res=mysql_real_connect(mysql,   //链接数据库
                                  "localhost",
                                  "root",
                                  "123",
                                  "userInfo",
                                  0,NULL,0);
    if(res==NULL){       //判断数据库链接
        fprintf(stderr,"%s\n",mysql_error(mysql));
        exit(-1);
    }
    char sql[4096]={0};      //用来存储select命令
    
    sprintf(sql,"update userInfo set salt = '%s' where username = '%s';",salt,username);
    printf("测试sql = %s\n",sql);
    int qres = mysql_query(mysql,sql);
    if(qres != 0)
    {
        fprintf(stderr,"%s:%s\n","mysql_query",mysql_error(mysql));
        exit(-1);
    }
    mysql_close(mysql);
    return 0;
}
int mysql_userInfo_Insert_encryted_password(char *encrypted_password,char *username)
{ 
  
      MYSQL *mysql= mysql_init(NULL);   //申请空间                            
      MYSQL *res=mysql_real_connect(mysql,   //链接数据库
                                    "localhost",
                                    "root",
                                    "123",
                                    "userInfo",
                                    0,NULL,0);
      if(res==NULL){       //判断数据库链接
          fprintf(stderr,"%s\n",mysql_error(mysql));
          exit(-1);
      }   
      char sql[4096]={0};      //用来存储select命令
      sprintf(sql,"update userInfo set encrypted_password = '%s' where username = '%s';",encrypted_password,username);
      int qres = mysql_query(mysql,sql);
      if(qres != 0)
      {   
          fprintf(stderr,"%s:%s\n","mysql_query",mysql_error(mysql));
          exit(-1);
      }   
      mysql_close(mysql);
      return 0;
}

int mysql_userInfo_takeOut_name(char *recvUseName,char *tempbuf)                                                                   
{
    //memset(tempbuf,0,2048);
    bzero(tempbuf,1024);
    MYSQL *mysql = mysql_init(NULL);
    MYSQL *res = mysql_real_connect(mysql,
                                    "localhost",
                                    "root",
                                    "123",
                                    "userInfo",
                                    0,NULL,0);
    if(res == NULL)
    {
        fprintf(stderr,"%s\n",mysql_error(mysql));
        exit(-1);
    }
    char sql[4096] = {0};//存select命令
    printf("username = %s\n", recvUseName);
    sprintf(sql,"select username from userInfo where username = '%s' and is_deleted = 0;",recvUseName);

    /* printf("buf = %s\n",sql); */
    mysql_query(mysql,sql);
    MYSQL_RES *table = mysql_store_result(mysql);
    char takeoutName[4096] = {0};//存查找到的数据结果
    for(unsigned int i = 0;i<mysql_num_rows(table);i++)
    {
        MYSQL_ROW row = mysql_fetch_row(table);
        for(unsigned int j = 0;j<mysql_num_fields(table);j++)
        {
            strcat(takeoutName,row[j]);
        }
    }
    /* printf("测试：takeoutName = %s\n",takeoutName); */
    strcpy(tempbuf,takeoutName);

    mysql_close(mysql);

    if(strlen(tempbuf)> 0)
    {
        printf("find success!\n");
        return 1;
    }
    printf("no username!\n");
    return 0;
}

int mysql_userInfo_takeOut_salt(char *username,char *tempbuf)                                                                   
{
    memset(tempbuf,0,4096);
    MYSQL *mysql = mysql_init(NULL);
    MYSQL *res = mysql_real_connect(mysql,
                                    "localhost",
                                    "root",
                                    "123",
                                    "userInfo",
                                    0,NULL,0);
    if(res == NULL)
    {
        fprintf(stderr,"%s\n",mysql_error(mysql));
        exit(-1);
    }
    char sql[4096] = {0};//存select命令

    sprintf(sql,"select salt from userInfo where username = '%s' and is_deleted = 0;",username);

    /* printf("buf = %s\n",sql); */
    mysql_query(mysql,sql);
    MYSQL_RES *table = mysql_store_result(mysql);
    
    if (table == NULL) {
    fprintf(stderr, "mysql_store_result 失败: %s\n", mysql_error(mysql));
    mysql_close(mysql);
    return 0;
    }
    
    char takeoutSalt[4096] = {0};//存查找到的数据结果
    for(unsigned int i = 0;i<mysql_num_rows(table);i++)
    {
        MYSQL_ROW row = mysql_fetch_row(table);
    
        for(unsigned int j = 0;j<mysql_num_fields(table);j++)
        {
            if (row[j] != NULL) {  // 必须检查是否为 NULL
                strcat(takeoutSalt, row[j]);
            } else {
                printf("警告: row[%d] 是 NULL\n", j);
            }
        }
    }
    /* printf("测试：takeoutSalt = %s\n",takeoutSalt); */
    strcpy(tempbuf,takeoutSalt);

    mysql_close(mysql);

    if(strlen(tempbuf)> 0)
    {
        printf("find salt success!\n");
        /* printf("tempbuf = %s\n",tempbuf); */
        return 1;
    }
    printf("salt = NULL\n");
    return 0;
}
int mysql_userInfo_takeOut_encryted_password(char *username,char *tempbuf)
{ 
    memset(tempbuf,0,4096);
    MYSQL *mysql = mysql_init(NULL);
    MYSQL *res = mysql_real_connect(mysql,
                                    "localhost",
                                    "root",
                                    "123",
                                    "userInfo",
                                    0,NULL,0);
    if(res == NULL)
    {
        fprintf(stderr,"%s\n",mysql_error(mysql));
        exit(-1);
    }
    char sql[4096] = {0};//存select命令

    sprintf(sql,"select encrypted_password from userInfo where username = '%s' and is_deleted = 0;",username);

    /* printf("buf = %s\n",sql); */
    mysql_query(mysql,sql);
    MYSQL_RES *table = mysql_store_result(mysql);
    char takeout[4096] = {0};//存查找到的数据结果
    for(unsigned int i = 0;i<mysql_num_rows(table);i++)                                                                                  
    {
        MYSQL_ROW row = mysql_fetch_row(table);
        for(unsigned int j = 0;j<mysql_num_fields(table);j++)
        {
            strcat(takeout,row[j]);
        }
    }
    /* printf("测试：takeoutEncrypted_password = %s\n",takeout); */
    strcpy(tempbuf,takeout);

    mysql_close(mysql);

    if(strlen(tempbuf)> 0)
    {
        printf("find encrypted_password success!\n");
        /* printf("tempbuf = %s\n",tempbuf); */
        return 1;
    }
    printf("encrypted_password = NULL\n");
    return 0;
}

int mysql_userInfo_updata_isDeleted(char *username)
{ 
    MYSQL *mysql = mysql_init(NULL);
    MYSQL *res = mysql_real_connect(mysql,
                                    "localhost",
                                    "root",
                                    "123",
                                    "userInfo",
                                    0,NULL,0);
    if(res==NULL){       //判断数据库链接
        fprintf(stderr,"%s\n",mysql_error(mysql));
        exit(-1);
    }
    char sql[4096]={0};      //用来存储select命令
    sprintf(sql,"update userInfo set is_deleted = '%s' where username = '%s';","1",username);
    /* printf("测试sql:%s\n",sql); */
    int qres = mysql_query(mysql,sql);
    if(qres != 0)
    {
        fprintf(stderr,"%s:%s\n","mysql_query",mysql_error(mysql));
        exit(-1);
    }
    mysql_close(mysql);
    return 0;
}

/* int main(int argc,char *argv[]) */ 
/* { */ 
/*     char *username = "test_user"; */                                                            
    //---------插入名字-------------------
    /* int result = mysql_userInfo_Insert_userName(username); */           
    /* if(result == 0) */                                          
    /* { */                                                        
    /*     printf("success\n"); */                                 
    /* } */                                                        
    /* else */                                                     
    /* { */                                                        
    /*     printf("faile\n:result = %d\n",result); */              
    /* } */ 

    //----------插入盐值------------------------
    /* char *salt = get_salt(); */
    /* int result = mysql_userInfo_Insert_salt(salt,username); */

    /* if(result == 0) */
    /* { */
    /*     printf("success\n"); */
    /* } */
    /* else */
    /* { */
    /*     printf("faile\n:result = %d\n",result); */
    /* } */

    //-----------更新is_deleted---------
    /* char *username = "test_user"; */
    /* int result = mysql_userInfo_updata_isDeleted(username); */
    /* if(result == 0) */          
    /* { */                        
    /*     printf("success\n"); */ 
    /* } */                        
    /* else */                     
    /* { */                        
    /*     printf("faile\n:result = %d\n",result); */
    /* } */ 

    //---------插入密文密码-----------
    /* char encry[] = "1234"; */
    /* char username[] = "test_user"; */
    /* int result = mysql_userInfo_Insert_encryted_password(encry,username); */
    /* if(result == 0) */
    /* { */
    /*     printf("success\n"); */
    /* } */
    /* else */
    /* { */
    /*     printf("faile\n:result = %d\n",result); */
    /* } */



    //----------找名字-------------------
    /* char tempbuf[4096] = {0}; */                                                                  
    /* char recvUseName[] = "test_user"; */ 
    /* int ret = mysql_userInfo_takeOut_name(recvUseName,tempbuf); */            
    /* if(ret == 0 ) */                                                
    /* { */                                                            
    /*     printf("用户不存在\n"); */                                  
    /* } */                                                            
    /* if(ret == 1) */                                                 
    /* { */                                                            
    /*     printf("用户名：%s\n",tempbuf); */                                    
    /* } */   

    //----------找盐值----------------------------
    /* char tempbuf[4096]; */ 
    /* char username[] = "test_user"; */ 
    /* int ret = mysql_userInfo_takeOut_salt(username,tempbuf); */
    /* if(ret == 0) */
    /* { */ 
    /*     printf("salt is NULL\n"); */
    /* }else{ */
    /*     printf("test_user salt = %s\n",tempbuf); */ 
    /* } */

    //----------找密文密码------------------------
    /* char tempbuf[4096]; */ 
    /* char username[] = "test_user"; */ 
    /* int ret = mysql_userInfo_takeOut_encryted_password(username,tempbuf); */
    /* if(ret == 0) */
    /* { */ 
    /*     printf("salt is NULL\n"); */
    /* }else{ */
    /*     printf("test_user salt = %s\n",tempbuf); */ 
    /* } */

    /* return 0; */
 /* } */

